import md5 from "md5";
import { Message } from "element-ui";

/**
 * 加密
 * @param {*} s
 * @returns
 */
export const encry = (s) => md5(s);

/**
 * 序列化
 * @param {*} s
 * @returns
 */
export const stringify = (s) => JSON.stringify(s);

/**
 * 反序列化
 * @param {*} s
 * @returns
 */
export const parse = (s) => JSON.parse(s);

/**
 * 存放到localStore
 * @param {*} data
 */
export const setLocal = (key, value) => {
  localStorage.setItem(key, stringify(value));
};

/**
 * 获取localStore的值
 * @param {*} key
 * @returns
 */
export const getLocal = (key) => parse(localStorage.getItem(key));

/**
 * 删除localStore的某一项
 */
export const delLocal = (key) => localStorage.removeItem(key);

/**
 * 成功的消息
 * @param {*} message
 * @returns
 */
export const success = (msg) => Message.success(msg);

/**
 * 失败的消息
 * @param {*} msg
 * @returns
 */
export const error = (msg) => Message.error(msg);

/**
 * 警告的消息
 * @param {*} msg
 * @returns
 */
export const warn = (msg) => Message.warning(msg);

/**
 * 提示消息
 * @param {*} msg
 * @returns
 */
export const info = (msg) => Message.info(msg);

/**
 * 获取时间
 * @param {*} time
 * @returns
 */
const fTime = (time) => {
  const date = new Date(time);

  return `${date.getFullYear()}-${date
    .getMonth()
    .toString()
    .padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")} ${date
    .getHours()
    .toString()
    .padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}:${date
    .getSeconds()
    .toString()
    .padStart(2, "0")}`;
};

/**
 * 格式化班级
 */
export const formatTime = (data) => {
  data.forEach((item) => {
    item.create_time = fTime(item.create_time);
  });
  return data;
};

/**
 *
 * @param {*} data
 */
export const formatPaperData = (data) => {
  data.forEach((item) => {
    item.create_time = fTime(item.create_time);
    item.className = item.classInfo.name;
  });
  return data;
};

/**
 * 格式化题目
 * @param {*} ss
 * @returns
 */
export const formatSubjects = (ss) => ss.filter((s) => !!s.id);

/**
 * 评卷
 * @param {*} form
 * @param {*} ans
 */
export const validate = (form, ans) => {
  const ret = [];

  let corr = 0,
    err = 0;
  form.forEach((f, i) => {
    const item = ans[i];
    if (f === item.ans.toUpperCase()) {
      ret.push({
        id: item.id,
        corr: true,
        err: false,
        ans: item.ans.toUpperCase(),
        uAns: f,
      });
      corr++;
    } else {
      err++;
      ret.push({
        id: item.id,
        corr: false,
        err: true,
        ans: item.ans.toUpperCase(),
        uAns: f,
      });
    }
  });

  return {
    ret,
    corr,
    err,
  };
};

/**
 * 格式化答题记录
 * @param {*} data
 */
export const formatSubjectRecord = (data) => {
  const set = new Set();
  const ret = [];

  data.forEach((item) => {
    const subjectId = item.subject_id;
    if (!set.has(subjectId)) {
      set.add(subjectId);
      ret.push({
        id: subjectId,
        name: item.name,
        options: item.options,
        errCount: item.ans !== item.uAns ? 1 : 0,
        corrCount: item.ans === item.uAns ? 1 : 0,
        [item.uAns]: 1,
      });
    } else {
      const obj = ret.find((d) => d.id === subjectId);
      obj.errCount = item.ans !== item.uAns ? obj.errCount + 1 : obj.errCount;
      obj.corrCount =
        item.ans !== item.uAns ? obj.corrCount + 1 : obj.corrCount;
      obj[item.uAns] = obj[item.uAns] === undefined ? 1 : obj[item.uAns] + 1;
    }
  });

  return ret;
};

/**
 * 格式化搜索结果
 * @param {*} data
 * @param {*} w
 */
export const formatSearchData = (data, w) => {
  data.forEach((item, i) => {
    item.name = item.name.replace(
      w,
      `<span><span class="hightlight">${w}</span><span/>`
    );
  });
};

/**
 *
 * @param {*} w
 */
export const formatWord = (result) => {
  let str = "";
  result.forEach((r) => {
    str += r.words;
  });

  return str.replace(/[^\u4E00-\u9FFF]*/g, "");
};

/**
 * 格式化教师首页信息
 */
export const formatIndexInfo = (data) => {
  data.subjects = data.subjects[0]?.count || 0;
  data.totalClass = data.totalClass[0]?.count || 0;
  let ret = [];
  const set = new Set();
  data.classInfo = data.classInfo.map((c) => ({
    name: c.name,
    value: c.count,
  }));
  data.paperInfo.forEach((d) => {
    if (!set.has(d.name)) {
      set.add(d.name);
      ret.push({ name: d.name, value: 1 });
    } else {
      const info = ret.find((r) => r.name === d.name);
      info.value += 1;
    }
  });
  data.paperInfo = ret;
  return data;
};

/**
 * 格式化教师试卷的详情
 * @param {*} data
 * @returns
 */
export const formatTchData = (data) => {
  data.score.forEach((s) => {
    s.name = (+s.name) + "分";
  });

  const ret = []
  const set = new Set()
  data.error.forEach(e => {
    if (!set.has(e.id)) {
      set.add(e.id)
      ret.push({id: e.id, count: e.ans !== e.uAns ? 1 : 0, name: e.name})
    } else {
      const item = ret.find(r => r.id === e.id)
      item.count += (e.ans !== e.uAns ? 1 : 0)
    }
  })

  const ret2 = []
  set.clear()

  data.subjects.forEach(s => {
    if (!set.has(s.name)) {
      set.add(s.name)
      const o = {
        A: 0,
        B: 0,
        C: 0,
        D: 0,
        name: s.name,
        ans: s.ans,
        options: s.options
      }
      o[s.uAns.toUpperCase()] += 1
      ret2.push(o)

    } else {
      const item = ret2.find(r2 => r2.name === s.name)
      item[s.uAns.toUpperCase()] += 1
    }
  })
  return {
    score: data.score,
    names: ret.map(r => r.name.slice(0, 8) + '...'),
    values: ret.map(r => r.count),
    subjects: ret2
  };
};
